// pages/wode.js
// pages/fenzhen/fenzhen.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    anxia1:false,
    anxia2:false,
    anxia3:false,
    anxia4:false,

    clickcall:false,

    kftel:'17794039424',
    userinfo: '',
    uqid: '',
    islogin: false,
  },

   //立即获取智能康复计划  && 授权
   bindGetUserInfo(e) {
    var that = this;
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var code = wx.getStorageSync("code");
      ajax('/api/index/login', {
        data: {
          js_code: code,
          nickname: e.detail.userInfo.nickName,
          avatar: e.detail.userInfo.avatarUrl
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
      }, res => {
        console.log(res)
        if (res.error == 0) {
          wx.setStorageSync("uqid", res.data.uqid)
          that.setData({
            uqid: res.data.uqid,
            session_key: res.data.session_key
          });
          that.getInfo(res.data.uqid)

        }else{ 
          wx.showToast({
            title: res,
            icon: 'none',
            duration: 2000
          })
          
        }
      })
    } else {
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }
  },

  getcallphone:function(){
    ajax("/api/index/contact_phone", {
      data: {},
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      console.log(res)
      if (res.error == 0) {
        this.setData({
          kftel: res.data.contact_phone
        });
      }
    })
   
  },

  refusecall: function () {
    this.setData({
      clickcall: false
    })
  },

  callphone: function () {
    this.setData({
      clickcall: true
    })
  },

  cancall: function () {
    this.setData({
      clickcall: false
    })
    wx.makePhoneCall({
      phoneNumber: this.data.kftel //仅为示例，并非真实的电话号码
    })
  },
  anxia(e){
    console.log(e)
    var index=e.currentTarget.dataset.index
    if(index==1){
      this.setData({
        anxia1:true
      })
    }
    if(index==2){
      this.setData({
        anxia2:true
      })
    }
    if(index==3){
      this.setData({
        anxia3:true
      })
    }
    if(index==4){
      this.setData({
        anxia4:true
      })
    }
  },
 
  dianji(e){
    // console.log(e)
    var index=e.currentTarget.dataset.index

    if(index==1){
      this.setData({
        anxia1:true
      })
    }
    if(index==2){
      this.setData({
        anxia2:true
      })
    }
    if(index==3){
      this.setData({
        anxia3:true
      })
    }
  
   
    if(index==1){
      if(this.data.islogin==true){
        wx.navigateTo({
          url: '../wodeyuyue/wodeyuyue',
        })
      }else{
        wx.showToast({
          title: '请先登录！',
          icon: 'none',
          duration: 1000
        })
        this.setData({
          anxia1:false
        })
      }
     
    }
    if(index==2){
      if(this.data.islogin==true){
        wx.navigateTo({
          url: '../bingli/bingli',
        })
      }else{
        wx.showToast({
          title: '请先登录！',
          icon: 'none',
          duration: 1000
        })
        this.setData({
          anxia2:false
        })
      }
      
    }
    if(index==3){
      wx.navigateTo({
        url: '../about/about',
      })
    }



  },
  touserinfo(){
    wx.navigateTo({
      url: '../personalinfo/personalinfo',
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcallphone()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      anxia1:false,
      anxia2:false,
      anxia3:false,
      anxia4:false,
    })


    var uqid = wx.getStorageSync('uqid');
    this.setData({
      uqid:uqid
    })
    if(uqid == ''||uqid==undefined){
      this.setData({
        islogin: false
      });
    }else{
      this.getInfo(uqid);
    }
  },

  getInfo: function (uqid){
    console.log(uqid)
    ajax("/api/index/user_info", {
      data: {
        uqid: uqid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
    }, res => {
      console.log(res);
      if (res.error == 0) {
        
        this.setData({
          userinfo: res.data,
          uqid: uqid,
          islogin: true
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})